package com.zygh.lz.controller;

import cn.hutool.core.util.IdUtil;
import net.sf.json.JSONArray;
import com.zygh.lz.dao.StaffMapper;
import com.zygh.lz.entity.Acceptance;
import com.zygh.lz.entity.BackInfo;
import com.zygh.lz.entity.Staff;
import com.zygh.lz.util.EffectiveHttp;
import com.zygh.lz.stapprove.ResultUtil;
import com.zygh.lz.vo.ResultBean;
import net.sf.json.JSONObject;
import org.apache.http.Header;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;


/**
 * 信大捷安认证，定制服务
 */
@RestController
public class XDJAController {

    /*private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private StaffMapper staffMapper;

    *//**
     * 用户凭证验证
     *
     * @param acceptance
     * @return
     *//*
    @PostMapping("/userAuthentication")
    public ResultBean userAuthentication(@RequestBody Acceptance acceptance, HttpServletRequest httpServletRequest) {

        //返回结果
        String message = "";
        //IF-UA-SVC-01验证用户凭证票据   用户凭证验证
        String URL = "http://62.9.41.88:9020/uas/v1/identifyController/indentifyUserCredential.do";
        //token参数
        String userCredential = acceptance.getUserCredential();
        String appCredential = acceptance.getAppCredential();
        String messageId = IdUtil.simpleUUID();
        String backInfo = "{\"backInfo\":\"0\"}";
        Map<String, Object> param = new HashMap<>();
        EffectiveHttp.ResponseWrap responseWrap = EffectiveHttp.createPost(URL).addHeader("userCredential", userCredential)
                .addHeader("appCredential", appCredential)
                .addHeader("messageId", messageId).addJsonBody(new BackInfo()).execute();

        int statusCode = responseWrap.getResponse().getStatusLine().getStatusCode();
        System.out.println("==========statusCode" + statusCode);
        if (statusCode != 200) {
            logger.error("调用用户凭据接口请求失败！");
            //throw new ServiceException("调用用户凭据接口请求失败");
        }
        String code = responseWrap.getResponse().getFirstHeader("code").getValue();
        if (code != null) {
            Header header = responseWrap.getResponse().getFirstHeader("message");

            System.out.println("headder=======" + header);
            if (header != null) {
                try {
                    message = URLDecoder.decode(header.getValue(), "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.getStackTrace();
                }
            }
        }
        Staff staff=new Staff();
        JSONArray json = JSONArray.fromObject(message); // 首先把字符串转成 JSONArray  对象
        if(json.size()>0){
            for(int i=0;i<json.size();i++){
                JSONObject job = json.getJSONObject(i);  // 遍历 jsonarray 数组，把每一个对象转成 json 对象
                System.out.println("=======personCode:"+job.get("personCode")) ;  // 得到 每个对象中的属性值
                String personCode = job.get("personCode").toString();
                String personIdentifier = job.get("personIdentifier").toString();
                String token = personCode+","+personIdentifier;
                System.out.println("=========token=======:"+token);
                LinkedHashMap<String, Object> jsons = new LinkedHashMap<>();
                staff = staffMapper.selectStaffByNum(personCode);
                jsons.put("token", token);
                jsons.put("staff", staff);

            }
        }
        return ResultUtil.setOK("success",json);


    }


    *//**
     * 应用凭证验证
     *
     * @param acceptance
     * @return
     *//*
    @PostMapping("/applyAuthentication")
    public void applyAuthentication(@RequestBody Acceptance acceptance, HttpServletRequest httpServletRequest) {

        //返回结果
        String message = "";
        //IF-UA-SVC-02验证应用凭证票据   应用凭证验证
        String URL = "http://62.9.41.88:9020/uas/v1/identifyController/indentifyAppCredential.do";
        //token参数
        String userCredential = acceptance.getUserCredential();
        String appCredential = acceptance.getAppCredential();
        String messageId = IdUtil.simpleUUID();
        String backInfo = "{\"backInfo\":\"0\"}";
        Map<String, Object> param = new HashMap<>();
        System.out.println("地址：" + URL);
        EffectiveHttp.ResponseWrap responseWrap = EffectiveHttp.createPost(URL).addHeader("userCredential", userCredential)
                .addHeader("appCredential", appCredential)
                .addHeader("messageId", messageId).addJsonBody(new BackInfo()).execute();

        int statusCode = responseWrap.getResponse().getStatusLine().getStatusCode();
        System.out.println("==========statusCode" + statusCode);
        if (statusCode != 200) {
            logger.error("调用用户凭据接口请求失败！");
            //throw new ServiceException("调用用户凭据接口请求失败");
        }
        String code = responseWrap.getResponse().getFirstHeader("code").getValue();
        System.out.println("=========code:" + code);
        if (code != null) {
            Header header = responseWrap.getResponse().getFirstHeader("message");

            if (header != null) {
                try {
                    message = URLDecoder.decode(header.getValue(), "utf-8");
                    System.out.println("message：" + message);
                } catch (UnsupportedEncodingException e) {
                    e.getStackTrace();
                }
            }
        }


    }*/
}
